package com.acwing.partition11;

import java.io.*;

/**
 * @author `RKC`
 * @date 2021/12/10 10:21
 */
public class AC1027方格取数 {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split("\\s+");
        int n = Integer.parseInt(s[0]);
        int[][] matrix = new int[n + 1][n + 1];
        while (true) {
            s = reader.readLine().split("\\s+");
            int row = Integer.parseInt(s[0]), col = Integer.parseInt(s[1]), val = Integer.parseInt(s[2]);
            if (row == 0 && col == 0 && val == 0) break;
            matrix[row][col] = val;
        }
        writer.write(dynamicProgramming(matrix) + "\n");
        writer.flush();
    }

    private static int dynamicProgramming(int[][] matrix) {
        int n = matrix.length;
        //dp[k][i1][i2]表示两个人同时走，距离起点一共走了k的路程，第一个人某个方向走了i1，另一个方向走了k-i1；第二个人某几个方向走了i2，另一个方向走了k-i2
        int[][][] dp = new int[2 * n][n][n];
        for (int k = 2; k <= 2 * n - 2; k++) {
            for (int i1 = 1; i1 < n; i1++) {
                for (int i2 = 1; i2 < n; i2++) {
                    int j1 = k - i1, j2 = k - i2;
                    //边界判断，任何时候都不能越界
                    if (j1 < 1 || j1 >= n || j2 < 1 || j2 >= n) continue;
                    //如果是走的同一个格子，只能计算一次，如果不是同一个格子，分别加上对应格子的数量
                    int val = i1 == i2 ? matrix[i1][j1] : matrix[i1][j1] + matrix[i2][j2];
                    dp[k][i1][i2] = Math.max(dp[k][i1][i2], max(dp[k - 1][i1 - 1][i2 - 1], dp[k - 1][i1 - 1][i2],
                            dp[k - 1][i1][i2 - 1], dp[k - 1][i1][i2]) + val);
                }
            }
        }
        return dp[2 * n - 2][n - 1][n - 1];
    }

    private static int max(int... nums) {
        int answer = -0x3f3f3f3f;
        for (int num : nums) answer = Math.max(answer, num);
        return answer;
    }
}
